package com.efast.cafe.portal.bean.query;

import com.efast.cafe.framework.util.QueryStringUtil;
import com.efast.cafe.framework.util.SqlConstants.FieldType;
import com.efast.cafe.framework.util.SqlConstants.OpType;

/**
 * 数字类型
 * @author Administrator
 *
 */
public class NumberFieldType extends AbstractFieldType {

	/**
	 * 
	 */
	private static final long serialVersionUID = 235736600080426871L;
	private static Integer count = 0;
	/**
	 * 处理Number数据类型（（key op value 逻辑符 。。））
	 * @param t（value以~分隔）
	 * @return
	 */
	@Override
	public <T extends CommonTableConditionBean> T getHandledObject(QueryParameterBean p,T t) {
		String combineSql = "";
		String value1 = "";
		String value2 = "";
		CommonTableConditionBean bean1 = null;
		CommonTableConditionBean bean2 = null;
		if (OpType.between_in.equals(t.getOp()) || OpType.between_not.equals(t.getOp())) {
			String[] array = t.getValue().split("~");
			bean1 = new CommonTableConditionBean();
			bean2 = new CommonTableConditionBean();
			if (array.length > 1) {
				value1 = array[0];
				value2 = array[1];
				if (!QueryStringUtil.isValidNumber(value1) || !QueryStringUtil.isValidNumber(value2)) {
					return t;
				}
				
				Integer opt1 = p.getIndex() + count;
				Integer opt2 = p.getIndex() + count +1;
				bean1.setValue(QueryStringUtil.getSqlDefaultValue(opt1.toString()));
				bean2.setValue(QueryStringUtil.getSqlDefaultValue(opt2.toString()));
				bean1.setRealValue(changeJavaType(value1));
				bean2.setRealValue(changeJavaType(value2));
				bean1.setKey(t.getKey());
				bean2.setKey(t.getKey());
			}
		}
		StringBuffer buffer = new StringBuffer();
		if (OpType.is.equals(t.getOp()) || OpType.is_not.equals(t.getOp())) {
			t.setOpType(OpType.getOp(t.getOp()));
			t.setValue("NULL");
			buffer.append(contactSql(p,t,t.getKey()));
		} else if (OpType.between_in.equals(t.getOp())) {
			bean1.setOpType(OpType.getOp(OpType.ge));
			bean2.setOpType(OpType.getOp(OpType.le));
			buffer.append(handleCommonData(p,bean1,bean2));
		} else if (OpType.between_not.equals(t.getOp())) {
			bean1.setOpType(OpType.getOp(OpType.gt));
			bean2.setOpType(OpType.getOp(OpType.lt));
			buffer.append(handleCommonData(p,bean1,bean2));
		} else {
			t.setOpType(OpType.getOp(t.getOp()));
			t.setRealValue(changeJavaType(t.getValue()));
			t.setValue(QueryStringUtil.getSqlDefaultValue(p.getIndex().toString()));
			buffer.append(contactSql(p,t,t.getKey()));
		}
		combineSql = buffer.toString();
		t.setCombineSql(combineSql);
		return t;
	}
	
	public Object changeJavaType(String value) {
		if (value.contains(".")) {
			return Double.parseDouble(value);
		} else {
			return Integer.parseInt(value);
		}
	}
	
	public <T extends CommonTableConditionBean> String handleCommonData(QueryParameterBean p,T t1,T t2) {
		String sqlOne = contactSql(p,t1,t1.getKey());
		String sqlTwo = contactSql(p,t2,t2.getKey());
		StringBuffer buffer = new StringBuffer();
		buffer.append(sqlOne).append(" AND ").append(sqlTwo);
		return buffer.toString();
	}
	
	
}
